package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao extends BaseStudentDao {
    private static Student[] stus = new Student[5];
    static {
        Student stu1 = new Student("12","张三","24","1996-5-1");
        Student stu2 = new Student("15","李四","30","1990-6-1");
        stus[0] = stu1;
        stus[1] = stu2;
    }
//添加
    @Override
    public boolean addStudent(Student st) {

        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null) {
                index = i;
                //如果为null,让index记录当前索引位置，并用break结束for循环。
                break;
            }
        }
        //返回是否添加成功
        if (index == -1) {
            return false;
        } else {
            stus[index] = st;
            return true;
        }

    }
//查看
    public Student[] findAllStudent() {
        return stus;
    }
//删除
    public void deleteStudentByld(String id) {
        int index = getIndex(id);
        stus[index] = null;

    }
//修改
    public void updateStudent(String id1, Student st) {
           int index = getIndex(id1);
           stus[index] = st;
        }
//查看索引
  public int getIndex(String id){
        int index = -1 ;
      for (int i = 0; i < stus.length; i++) {
          if (stus[i] != null && stus[i].getId().equals(id)){
              index=i;
              break;
          }
      }
      return index;
  }
}